﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_ganimais : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Utilizador util3;
        public Cliente cli3;
        public Animal an4;

        public int idc;

        
        public frm_ganimais(Utilizador vet)
        {
            this.util3 = vet;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void pesquisa_cliente()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            int idv = util3.get_id_vet();
            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT (c.nome) as cliente, id_animal, (an.nome) as nomea, especie, chip, data_nascimento, sexo, esterilizado, imagem FROM Animais an INNER JOIN Cliente c ON an.id_cliente = c.id_cliente WHERE c.nome LIKE '%" + txtga_dono.Text + "%' and c.id_veterinario = "+ idv +" and an.oculto = 0";
            SqlDataReader readatrib = command1.ExecuteReader();
            
            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["cliente"].ToString();
                this.dataGridView1[1, i].Value = readatrib["id_animal"].ToString();
                this.dataGridView1[2, i].Value = readatrib["nomea"].ToString();
                this.dataGridView1[3, i].Value = readatrib["especie"].ToString();
                this.dataGridView1[4, i].Value = readatrib["chip"].ToString();
                this.dataGridView1[5, i].Value = readatrib["data_nascimento"].ToString();
                this.dataGridView1[6, i].Value = readatrib["sexo"].ToString();
                this.dataGridView1[7, i].Value = readatrib["esterilizado"].ToString();
                this.dataGridView1[8, i].Value = readatrib["imagem"].ToString();

            }
            con.Close();
        }

        private void pesquisa_animal()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            int idv = util3.get_id_vet();
            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT (c.nome) as cliente, id_animal, (an.nome) as nomea, especie, chip, data_nascimento, sexo, esterilizado, imagem FROM Animais an INNER JOIN Cliente c ON an.id_cliente = c.id_cliente WHERE an.nome LIKE '%" + txtga_nome.Text + "%' and c.id_veterinario = " + idv + " and an.oculto = 0";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["cliente"].ToString();
                this.dataGridView1[1, i].Value = readatrib["id_animal"].ToString();
                this.dataGridView1[2, i].Value = readatrib["nomea"].ToString();
                this.dataGridView1[3, i].Value = readatrib["especie"].ToString();
                this.dataGridView1[4, i].Value = readatrib["chip"].ToString();
                this.dataGridView1[5, i].Value = readatrib["data_nascimento"].ToString();
                this.dataGridView1[6, i].Value = readatrib["sexo"].ToString();
                this.dataGridView1[7, i].Value = readatrib["esterilizado"].ToString();
                this.dataGridView1[8, i].Value = readatrib["imagem"].ToString();

            }
            con.Close();
        }

        private void cmdgc_ia_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int i = this.dataGridView1.CurrentRow.Index;
                string temp_ida = this.dataGridView1.Rows[i].Cells["id_animal"].Value.ToString();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_animal, nome, especie, chip, data_nascimento, sexo, esterilizado, imagem FROM Animais WHERE id_animal = '" + temp_ida + "' and oculto = 0";
                SqlDataReader readatrib = command1.ExecuteReader();
                while (readatrib.Read())
                {
                    an4 = new Animal(Convert.ToInt32(readatrib["id_animal"].ToString()), readatrib["nome"].ToString(), readatrib["especie"].ToString(), readatrib["chip"].ToString(), readatrib["data_nascimento"].ToString(), readatrib["sexo"].ToString(), null, readatrib["imagem"].ToString(), readatrib["esterilizado"].ToString());
                }
                con.Close();

                string temp_nc = this.dataGridView1.Rows[i].Cells["cliente"].Value.ToString();
                
                //frm_novaconsulta frm4 = new frm_novaconsulta(an4, temp_nc);
                //frm4.ShowDialog();
                frm_consultas frm4 = new frm_consultas(an4, temp_nc, "animal", 0);
                frm4.ShowDialog();

            }
            else
            {
                MessageBox.Show("Tem de selecionar um animal", "Selecione um registo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmdga_clientep_Click(object sender, EventArgs e)
        {
            txtga_nome.ResetText();
            try
            {
                pesquisa_cliente();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdga_animalp_Click(object sender, EventArgs e)
        {
            txtga_dono.ResetText();
            try
            {
                pesquisa_animal();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdga_insanimal_Click(object sender, EventArgs e)
        {
            frm_ianimal1 frm1 = new frm_ianimal1(util3);
            frm1.ShowDialog();
        }

        private void cmdga_voltar_Click(object sender, EventArgs e)
        {
            frm_gesclinica frm2 = new frm_gesclinica(util3);
            this.Close();
            frm2.ShowDialog();
        }

        private void cmdga_as_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int i = this.dataGridView1.CurrentRow.Index;
                int idv = util3.get_id_vet();
                string temp_nc = this.dataGridView1.Rows[i].Cells["cliente"].Value.ToString();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome = '" + temp_nc + "' and id_veterinario = " + idv + " and oculto = 0";
                SqlDataReader readatrib = command1.ExecuteReader();
                while (readatrib.Read())
                {
                    cli3 = new Cliente(Convert.ToInt32(readatrib["id_cliente"].ToString()), readatrib["nome"].ToString(), readatrib["morada"].ToString(), Convert.ToInt32(readatrib["cod_postal"].ToString()), readatrib["localidade"].ToString(), Convert.ToInt32(readatrib["telemovel"].ToString()), readatrib["email"].ToString());
                }

                Animal an2 = new Animal(Convert.ToInt32(dataGridView1["id_animal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nomea", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["especie", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["chip", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["data_nascimento", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["sexo", dataGridView1.CurrentRow.Index].Value.ToString(), null, dataGridView1["imagem", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["esterilizado", dataGridView1.CurrentRow.Index].Value.ToString());

                frm_ianimal frm3 = new frm_ianimal(cli3, an2, "editar");
                frm3.ShowDialog();
            }
            else
                MessageBox.Show("Tem de selecionar uma linha!", "Por favor, selecione", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void cmdga_eliminar_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int i = this.dataGridView1.CurrentRow.Index;
                int idv = util3.get_id_vet();
                string temp_nc = this.dataGridView1.Rows[i].Cells["cliente"].Value.ToString();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome = '" + temp_nc + "' and id_veterinario = " + idv + " and oculto = 0";
                SqlDataReader readatrib = command1.ExecuteReader();
                while (readatrib.Read())
                {
                    cli3 = new Cliente(Convert.ToInt32(readatrib["id_cliente"].ToString()), readatrib["nome"].ToString(), readatrib["morada"].ToString(), Convert.ToInt32(readatrib["cod_postal"].ToString()), readatrib["localidade"].ToString(), Convert.ToInt32(readatrib["telemovel"].ToString()), readatrib["email"].ToString());
                }
                Animal an3 = new Animal(Convert.ToInt32(dataGridView1["id_animal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nomea", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["especie", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["chip", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["data_nascimento", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["sexo", dataGridView1.CurrentRow.Index].Value.ToString(), null, dataGridView1["imagem", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["esterilizado", dataGridView1.CurrentRow.Index].Value.ToString());
                con.Close();

                frm_ga_eliminaran frm4 = new frm_ga_eliminaran(an3, cli3);
                frm4.ShowDialog();
            }
            else
                MessageBox.Show("Tem de selecionar uma linha!", "Por favor, selecione", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
